package app;

import java.util.ArrayList;

public class SearchManager {
	
	private DocumentManager doc = new DocumentManager();
	private CLI cli = new CLI();
	
	public SearchManager()
	{
		setupSearch();
	}
	
	public void setupSearch()
	{
		int type = cli.getInt("What kind of search do you want? 1- Within Text.    2- Between Text.");
		ArrayList<Essay> searchList = new ArrayList<Essay>();
		ArrayList<Essay> essays = new ArrayList<Essay>();
		int choice = 0;
		switch(type){
		case 1:
			//Within Text Search
			displaySubjects();
			ArrayList<search.WithinResult> withinResults = new ArrayList<search.WithinResult>();
			essays = getEssays(cli.getString("Enter the usbject you wish to use:")); //Get the subject of the essays
			//Show the essays
			for(int i=0;i<essays.size();i++)
				System.out.print(i + ". " + (String)essays.get(i).subject + " by " + (String)essays.get(i).author + "\t");
			System.out.println();
			//Select essay
			choice = cli.getInt("Enter the number of the essay you want to choose: ");
			searchList.add(essays.get(choice));
			withinResults = new search.WithinTextSearch(searchList).search();
			new ReportManager(searchList,withinResults,0,0).analyzeWithin();			
		break;
		case 2:
			//Between Text Search
			displaySubjects();
			searchList = new ArrayList<Essay>();
			ArrayList<search.BetweenResult> betweenResults = new ArrayList<search.BetweenResult>();
			essays = getEssays(cli.getString("Enter the subject you wish to use:")); //Get the subject of the essays
			//Show the essays
			for(int i=0;i<essays.size();i++)
				System.out.print(i + ". " + (String)essays.get(i).subject + " by " + (String)essays.get(i).author + "\t");
			System.out.println();
			//Select essays
			int more = 1;
			while(more > 0){
				choice = cli.getInt("Enter the number of the essay you want to choose: ");
				searchList.add((Essay)essays.get(choice));
				more = cli.getInt("Add another document? 1- Yes.    0- No.");
				if((more == 0) && (searchList.size() < 2)){
					System.out.println("You need at least 2 Documents for a Between Text search.");
					more = 1;
				}
			}
			int severity = cli.getInt("How pedantic should I be with searches (0 = very, 1 = less very, 2 = not very, 3 = Plagiarism R' Us!");
			//Fire off the Search
			betweenResults = new search.BetweenTextSearch(severity,searchList).search();
			new ReportManager(searchList,betweenResults).analyzeBetween();
		break;
		default:
			System.out.println("Invalid Search Type.");
			setupSearch();
		break;
		}
	}
	
	/**
	 * Get the list of Subjects to choose from.
	 */
	public void displaySubjects()
	{
		ArrayList<String> subjects = doc.getAllSubjects();
		for(int i=0;i<subjects.size();i++)
			System.out.print(subjects.get(i) + "\t");
		System.out.println();
	}
	
	/**
	 * Get the essays under a particular subject
	 */
	public ArrayList<Essay> getEssays(String subject)
	{
		return doc.getEssays(subject);
	}
}
